/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.functionnal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

/**
 * Selenium functional test of Event Dispatching on the OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 26 January 2011
 */
public class SeleniumTestEventDispatching {

    @Test
    public void testEventDispatching() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        String baseUrl = "http://127.0.0.1:8080/";
        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
        
        // load the page and test it's loaded
        selenium.open("/index.html");
        selenium.waitForPageToLoad("30000");
        assertTrue(selenium.isElementPresent("//body[@id='fr-openparts']"));
        assertTrue(selenium.isTextPresent("OpenParts Showcase"));
        
        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Event Dispatching"));
        
        // load the event general module and module 1, and test they are loaded
        selenium.click("//body[@id='fr-openparts']/div[2]/div[2]/div/div[2]/div/button[2]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Event Dispatch Module"));
        assertTrue(selenium.isTextPresent("Module 1 View"));
        
        // check that module 2 is not loaded yet
        assertFalse(selenium.isTextPresent("Module 2 View"));
        
        // check that no event where received yet
        assertFalse(selenium.isTextPresent("module 1 received event with"));
        assertFalse(selenium.isTextPresent("module 2 received event with"));
        
        // dispatch an event to module 1 - test that only module 1 get it, and that module 2 is still not loaded
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[1]");
        assertTrue(selenium.isTextPresent("module 1 received event with param 1"));
        assertFalse(selenium.isTextPresent("Module 2 View"));
        assertFalse(selenium.isTextPresent("module 2 received event with param 1"));
        
        // dispatch an event to module 2 - test that module 1 didn't get it, and that module 2 is still not loaded
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[2]");
        Thread.sleep(2000);
        assertFalse(selenium.isTextPresent("module 1 received event with param 2"));
        assertFalse(selenium.isTextPresent("Module 2 View"));
        assertFalse(selenium.isTextPresent("module 2 received event with param 2"));
        
        // dispatch an event to both modules - test that module 1 get it, and that module 2 is still not loaded
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[3]");
        assertTrue(selenium.isTextPresent("module 1 received event with param 3"));
        assertFalse(selenium.isTextPresent("Module 2 View"));
        assertFalse(selenium.isTextPresent("module 2 received event with param 3"));
        
        // load module 2 and display it - test that module 2 is displayed, and also that it did received message 2 and 3, but not message 1
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[4]");
        Thread.sleep(1000);
        assertTrue(selenium.isTextPresent("Module 2 View"));
        assertFalse(selenium.isTextPresent("module 2 received event with param 1"));
        assertTrue(selenium.isTextPresent("module 2 received event with param 2"));
        assertTrue(selenium.isTextPresent("module 2 received event with param 3"));
        
        // dispatch an event to module 1 - test that only module 1 get it
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[5]");
        assertTrue(selenium.isTextPresent("module 1 received event with param 4"));
        assertFalse(selenium.isTextPresent("module 2 received event with param 4"));
        
        // dispatch an event to module 2 - test that only module 2 get it
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[6]");
        assertFalse(selenium.isTextPresent("module 1 received event with param 5"));
        assertTrue(selenium.isTextPresent("module 2 received event with param 5"));
        
        // dispatch an event to both modules - test that both modules get it
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[7]");
        assertTrue(selenium.isTextPresent("module 1 received event with param 6"));
        assertTrue(selenium.isTextPresent("module 2 received event with param 6"));
        
        // dispatch an event for which only module 1 have a specific handler - test that module 1 get it throught its specific handler, and that module 2 get it through its generic handler
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[8]");
        assertTrue(selenium.isTextPresent("module 1 received specific mod1"));
        assertTrue(selenium.isTextPresent("module 2 received generic mod1"));
        
        // dispatch a event for which only module 2 have a specific handler - test that module 1 get it throught its generic handler, and that module 2 get it through its specific handler
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[9]");
        assertTrue(selenium.isTextPresent("module 1 received generic mod2"));
        assertTrue(selenium.isTextPresent("module 2 received specific mod2"));
        
        // dispatch an event for which neither module have a specific handler - test that both get it through their generic handler
        selenium.click("//div[@id='OP-eventmodule-eventmodule-display1']/div/div[4]/div/button[10]");
        assertTrue(selenium.isTextPresent("module 1 received generic mod"));
        assertTrue(selenium.isTextPresent("module 2 received generic mod"));
        
        selenium.stop();
    }
}
